package com.calligraphy.product.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.calligraphy.bases.utils.KeyUtil;
import com.calligraphy.bases.utils.SetMsg;
import com.calligraphy.product.exception.ProductException;
import com.calligraphy.product.form.ProductForm;
import com.calligraphy.product.model.ProductCategory;
import com.calligraphy.product.model.ProductInfo;
import com.calligraphy.product.service.CategoryService;
import com.calligraphy.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 卖家商品
 * @Author hy
 * @Date 2019/1/11 10:13
 **/

@Slf4j
@RestController
@RequestMapping("/seller/product")
public class SellerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 保存/更新
     * @param form
     * @param bindingResult
     * @return
     */
    @PostMapping("/save")
//    @CachePut(cacheNames = "product",key = "123")
    @CacheEvict(cacheNames = "product",key = "123")
    public JSONObject save(@Validated ProductForm form,
                           BindingResult bindingResult){
        JSONObject json = new JSONObject();
        if(bindingResult.hasErrors()){
            json.put("result",bindingResult.getFieldError().getDefaultMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }

        ProductInfo productInfo = new ProductInfo();
        try{
            //如果productId不为空，说明是已有商品
            if(!StringUtils.isEmpty(form.getProductId())){
                productInfo = productService.findOne(form.getProductId());
            }else{
                form.setProductId(KeyUtil.genUniqueKey());
            }
            BeanUtils.copyProperties(form,productInfo);
            productService.save(productInfo);
        }catch (ProductException e){
            json.put("msg",e.getMessage());
            SetMsg.setMessage(json,1,"系统内部错误");
            return json;
        }
        SetMsg.setMessage(json,0,"success");
        return json;
    }

    @GetMapping("/list")
    public JSONObject list(@RequestParam(value = "page" ,defaultValue="1") Integer page,
                           @RequestParam(value = "size",defaultValue = "10") Integer size){

        JSONObject json = new JSONObject();
        PageRequest request = PageRequest.of(page-1,size);
        Page<ProductInfo> productInfoPage = productService.findAll(request);
        json.put("productInfoPage",productInfoPage);
        json.put("currentPage",page);
        json.put("size",size);
        SetMsg.setMessage(json,0,"success");
        return json;
    }

    /**
     * 商品上架
     * @param productId
     * @return
     */
    @PostMapping("/on_sale")
    public JSONObject onSale(@RequestParam("productId") String productId){
        JSONObject json = new JSONObject();
        try{
            productService.onSale(productId);
        }catch (ProductException e){
            json.put("msg",e.getMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }
        SetMsg.setMessage(json,0,"success");
        return json;
    }
    /**
     * 商品下架
     * @param productId
     * @return
     */
    @PostMapping("/off_sale")
    public JSONObject offSale(@RequestParam("productId") String productId){
        JSONObject json = new JSONObject();
        try{
            productService.offSale(productId);
        }catch (ProductException e){
            json.put("msg",e.getMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }
        SetMsg.setMessage(json,0,"success");
        return json;
    }

    @GetMapping("index")
    public JSONObject index(@RequestParam(value = "productId",required = false) String productId){
        JSONObject json = new JSONObject();
        if(!StringUtils.isEmpty(productId)){
            ProductInfo productInfo=productService.findOne(productId);
            json.put("productInfo",productInfo);
        }

        //查询所有的类目
        List<ProductCategory> categoryList=categoryService.findAll();
        json.put("categoryList",categoryList);
        SetMsg.setMessage(json,0,"success");
        return json;
    }

}
